package com.example.demo.api.controller.v1;

import com.example.demo.api.dto.DepartmentDTO;
import com.example.demo.domain.entity.Department;
import com.example.demo.service.DepartmentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/department")
public class DepartmentController {
    private final DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }


    /**
     * 一对多查询测试，查部门的所有用户
     * @param
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<?> list(){
        List<Department> list = departmentService.findDepartmentList();
        return ResponseEntity.ok(list);
    }

    /**
     * foreach插入测试
     * @param departments
     * @return
     */
    @PostMapping("/insertByList")
    public  ResponseEntity<?> insertEmployee(@RequestBody List<DepartmentDTO> departments){
        departmentService.insertByList(departments);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/{departmentId}")
    public ResponseEntity<?> getById(@PathVariable("departmentId") Long departmentId){
        Department department = departmentService.findDepartmentById(departmentId);
        return ResponseEntity.ok(department);
    }

    @GetMapping("/len")
    public ResponseEntity<?> getLen(){
        int len = departmentService.countDepartmentLen();
        return ResponseEntity.ok(len);
    }

    @PutMapping("/update")
    public ResponseEntity<?> updateEmployee(@RequestBody DepartmentDTO departmentDTO) {
        if (departmentDTO.getDepartmentId() == null) {
            return ResponseEntity.ok("[department_update]update error!");
        }
        departmentService.updateDepartment(departmentDTO);
        return ResponseEntity.ok(null);
    }


    @PutMapping("/insert")
    public ResponseEntity<?> insertEmployee(@RequestBody DepartmentDTO departmentDTO) {
        if (departmentDTO.getDepartmentId() == null) {
            return ResponseEntity.ok("[department_insert]insert error!");
        }
        departmentService.insertDepartment(departmentDTO);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> deleteEmployee(@RequestParam Long departmentId){
        if (departmentId == null) {
            return ResponseEntity.ok("[department_delete]delete error!");
        }
        departmentService.removeDepartment(departmentId);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/byListId")
    public ResponseEntity<?> listByIds(@RequestParam List<Long> departmentIds){
        List<Department> departmentList = departmentService.findDepartmentListByIdList(departmentIds);
        return ResponseEntity.ok(departmentList);
    }

}
